﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using DG.Tweening;
using Path = DG.Tweening.Plugins.Core.PathCore.Path;

namespace linkouqs.Gamekit {

    public sealed class CharacterAbilityTake : CharacterAbility {

        [Range(0f, 1f)]
        public float liftUpStoneYOffset = 0.2f;
        [Range(0.02f, 1f)]
        public float liftUpStoneDuraction = 0.3f;
        [Range(0f, 1f)]
        public float liftUpStoneDelay = 0.01f;

        public TakeableObject TakeableObject { get; private set; }
        public TakeableObject ThrowableObject { get; private set; }

        public override bool CanStartAction() {

            if (IsInAnimation) {
                return false;
            }

            if (TakeableObject == null) {
                //Debug.LogWarning("TakeableObject == null");
                return false;
            }

            if (!Character.CharacterSlots.HasSlot(TakeableObject.SlotName)) {
                //Debug.LogWarning(TakeableObject.SlotName);
                return false;
            }

            return Character.IsFaceTo(TakeableObject.transform);

        }

        public override void StartAction() {

            base.StartAction();

            Character.Animator.SetTrigger("Take");

            ThrowableObject = TakeableObject;

            Transform slot = Character.CharacterSlots.Attach(ThrowableObject.SlotName, ThrowableObject.transform);

            var arr = FindObjectsOfType<CharacterAbilityTake>();
            foreach (var c in arr) {
                c.OnUnselect(ThrowableObject);
            }

            ThrowableObject.OnTake();

            PlayTakeAnimation(slot, ThrowableObject.transform, StopAction);
        }

        void PlayTakeAnimation(Transform slot, Transform obj, System.Action callback) {
            Transform slot1 = new GameObject().transform;
            slot1.position = slot.position;
            Transform obj1 = new GameObject().transform;
            obj1.position = obj.position;

            Vector3 top = new Vector3(Vector3.Lerp(obj1.position, slot1.position, 0.5f).x, slot1.position.y + liftUpStoneYOffset, slot1.position.z);
            Vector3[] arr = new Vector3[3] { obj1.position, top, slot1.position };

            obj1.DOPath(new Path(PathType.CatmullRom, arr, 1), liftUpStoneDuraction).SetDelay(liftUpStoneDelay).SetEase(Ease.OutBack).OnUpdate(() => {
                obj.position = slot.position + (obj1.position - slot1.position);
            }).OnComplete(() => {
                Destroy(slot1.gameObject);
                Destroy(obj1.gameObject);
                obj.position = slot.position;
                callback?.Invoke();
            });

        }

        public void OnSelect(TakeableObject obj) {
            TakeableObject = obj;
        }

        public void OnUnselect(TakeableObject obj) {
            
                if(TakeableObject == obj) {
                    TakeableObject = null;
                }
        }

    }

}
